<?php
namespace app\common\listener;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
/**
 * 动态配置
 * Class RecommendCate
 * @package app\common\listener
 */
class DynamicConf
{
    public function handle(){
        //常量定义
        $this->initConst();
        //动态读取配置文件
        $this->initConfig();
    }
    /**
     *初始化常量
     */
    private function initConst(){
        // 初始化分层名称常量
        $this->initLayerConst();

        // 初始化结果常量
        $this->initResultConst();

        //初始化数据状态常量
        $this->initResultStatusConst();

        // 初始化时间常量
        $this->initTimeConst();

        // 初始化系统常量
        $this->initSystemConst();

        // 初始化路径常量
        $this->initPathConst();

    }
    /**
     * 初始化分层名称常量
     */
    private function initLayerConst(){
        defined('LAYER_LOGIC_NAME') or define('LAYER_LOGIC_NAME', 'logic');
        defined('LAYER_MODEL_NAME') or define('LAYER_MODEL_NAME', 'model');
        defined('LAYER_VALIDATE_NAME') or define('LAYER_VALIDATE_NAME', 'validate');
        defined('LAYER_SERVICE_NAME') or define('LAYER_SERVICE_NAME', 'service');
        defined('LAYER_CONTROLLER_NAME') or define('LAYER_CONTROLLER_NAME', 'controller');
        defined('LAYER_VIEW_NAME') or define('LAYER_VIEW_NAME', 'view');
    }

    /**
     * 初始化结果标识常量
     */
    private function initResultConst(){
        defined('RESULT_SUCCESS') or define('RESULT_SUCCESS', 'success');
        defined('RESULT_REDIRECT') or define('RESULT_REDIRECT', 'redirect');
        defined('RESULT_MESSAGE') or define('RESULT_MESSAGE', 'msg');
        defined('RESULT_URL') or define('RESULT_URL', 'url');
        defined('RESULT_DATA') or define('RESULT_DATA', 'data');
        defined('RESULT_STATUS') or define('RESULT_STATUS', 'status');
        defined('RESULT_CODE') or define('RESULT_CODE', 'code');
    }

    /**
     * 初始化数据状态常量
     */
    private function initResultStatusConst(){
        defined('RESULT_SUCCESS_STATUS') or define('RESULT_SUCCESS_STATUS', 200);
        defined('RESULT_REDIRECT_STATUS') or define('RESULT_REDIRECT_STATUS', 302);
        defined('RESULT_WRONG_STATUS') or define('RESULT_WRONG_STATUS', 400);
        defined('RESULT_ERROR_STATUS') or define('RESULT_ERROR_STATUS', 500);
        define('DATA_STATUS_NAME'       , 'status');
        define('DATA_NORMAL'            , 1);
        define('DATA_DISABLE'           , 0);
        define('DATA_DELETE'            , -1);
        define('DATA_SUCCESS'           , 1);
        define('DATA_ERROR'             , 0);
    }



    /**
     * 初始化时间常量
     */
    private function initTimeConst(){
        defined('TIME_CT_NAME') or define('TIME_CT_NAME', 'create_time');
        defined('TIME_UT_NAME') or define('TIME_UT_NAME', 'update_time');
        defined('TIME_NOW') or define('TIME_NOW', time());
    }

    /**
     * 初始化系统常量
     */
    private function initSystemConst(){
        $app_config = config('app');
        defined('SYS_APP_NAMESPACE') or define('SYS_APP_NAMESPACE', $app_config['app_namespace']);
        defined('SYS_DRIVER_DIR_NAME') or define('SYS_DRIVER_DIR_NAME', 'driver');
        defined('SYS_COMMON_DIR_NAME') or define('SYS_COMMON_DIR_NAME', 'common');
        defined('SYS_STATIC_DIR_NAME') or define('SYS_STATIC_DIR_NAME', 'static');
        defined('SYS_VERSION') or define('SYS_VERSION', '1.0.0');
        defined('SYS_ADMINISTRATOR_ID') or define('SYS_ADMINISTRATOR_ID', 1);
        defined('SYS_DS_PROS') or define('SYS_DS_PROS', '/');
        defined('SYS_DS_CONS') or define('SYS_DS_CONS', '\\');
        $database_config                = config('database');

        define('SYS_DB_PREFIX'          , $database_config['prefix']);
        define('SYS_ENCRYPT_KEY'        , $database_config['sys_data_key']);
    }

    /**
     * 初始化路径常量
     */
    private function initPathConst(){

        defined('DS') or define('DS', DIRECTORY_SEPARATOR);
        defined('ROOT_PATH') or define('ROOT_PATH', env('ROOT_PATH','/'));
        defined('PATH_PUBLIC') or define('PATH_PUBLIC', ROOT_PATH   . 'public'    . DS);
        defined('PATH_UPLOAD') or define('PATH_UPLOAD', PATH_PUBLIC . 'upload'    . DS);
        defined('PATH_PICTURE') or define('PATH_PICTURE',  PATH_UPLOAD . 'picture'   . DS);
        defined('PATH_FILE') or define('PATH_FILE', PATH_UPLOAD . 'file'      . DS);
        defined('PATH_SERVICE') or define('PATH_SERVICE', DS   . SYS_APP_NAMESPACE . DS . SYS_COMMON_DIR_NAME . DS . LAYER_SERVICE_NAME . DS);
        defined('PATH_COMMON_LOGIC') or define('PATH_COMMON_LOGIC', SYS_APP_NAMESPACE . SYS_DS_CONS . SYS_COMMON_DIR_NAME . SYS_DS_CONS . LAYER_LOGIC_NAME . SYS_DS_CONS);

    }

    /**
     * 初始化配置信息
     */
    private function initConfig(){
        $config_list = $this->getConfigList();
        $this->setConf($config_list);
        $this->initOtherconfig();
        $this->initTmconfig();
    }

    /**
     * 初始化动态配置信息
     */
    private function initTmconfig()
    {

        $list_rows                  = config('list_rows');
        $api_key                    = config('api_key');
        $jwt_key                    = config('jwt_key');
        $static_domain              = config('static_domain');

        define('DB_LIST_ROWS'       , empty($list_rows)                 ? 10        : $list_rows);
        define('API_KEY'            , empty($api_key)                   ? 'apibase' : $api_key);
        define('JWT_KEY'            , empty($jwt_key)                   ? 'apibase' : $jwt_key);
        define('STATIC_DOMAIN'      , empty($static_domain)             ? null      : $static_domain);
    }

    /**
     * 设置config
     * @param $config_list
     */
    private function setConf($config_list){
        $config_key = 'dynamic_conf';
        $config_array = [];
        foreach ($config_list as $info) {
            $config_array[$info['name']] = $info['value'];
        }
        Config::set($config_array,$config_key);
    }

    /**
     * 获取config列表
     * @return mixed|\think\Collection
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigList(){
        $redisKey = 'dynamic_config_list';
        //_p(Cache::store('redis')->has($redisKey));
        if(Cache::store('redis')->has($redisKey)){
            $config_list = Cache::store('redis')->get($redisKey);
        }else{
            $table_config = env('database.prefix').'config';
            $config_list = Db::table($table_config)->select();
            Cache::set($redisKey, $config_list, 30);
        }
        return $config_list;
    }

    /**
     * 初始化动态配置信息
     */
    private function initOtherconfig(){
        $dynamic_conf = config('dynamic_conf');
        define('PAGE'               , $dynamic_conf['page']?:1);
        define('PER_PAGE'           , $dynamic_conf['per_page']?:10);
        define('LIST_MAX'           , $dynamic_conf['max_list']?:5000);
        define('API_KEY'            , $dynamic_conf['api_key']?:'apikey');
        define('JWT_KEY'            , $dynamic_conf['jwt_key']?:'jwtkey');
        define('STATIC_DOMAIN'      , $dynamic_conf['static_domain']?:null);
        define('COOKIE_DOMAIN'      , $dynamic_conf['cookie_domain']?:'.dc.com');
    }
}
